package com.itheima.service;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.RoleDao;
import com.itheima.pojo.Role;
import entity.PageResult;
import entity.QueryPageBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description:
 * @Author: yp
 */
@Service(interfaceClass = RoleService.class)
@Transactional
public class RoleServiceImpl implements RoleService {

    @Autowired
    private RoleDao roleDao;

    /**
     * 根据用户id查询角色
     * @param userId
     * @return
     */
    @Override
    public Set<Role> findByUid(Integer userId) {
        return roleDao.findByUid(userId);
    }


    //角色分页查询:
    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        //1.调用MyBatis分页插件方法(自动帮我们做分页)  ThreadLocal, MyBatis拦截器
        PageHelper.startPage(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        //2.调用Dao
        Page<Role> page = roleDao.selectConditions(queryPageBean.getQueryString());
        PageResult pageResult = new PageResult(page.getTotal(), page.getResult());
        return pageResult;
    }


    //添加角色:
    @Override
    public void add(Role role, Integer[] checkitemIds, Integer[] checkitemIdsMenu) {

        System.out.println("菜单 选择框==="+checkitemIdsMenu);
        System.out.println("权限 选择框==="+checkitemIds);
        //1.调用Dao,保存CheckGroup【返回当前保存的id】
        roleDao.add(role);
        //2.调用Dao, 把选择的检查项和检查组的id保存到t_checkgroup_checkitem
        Integer roleId = role.getId();
        setCheckGroupAndCheckItem(roleId,checkitemIds);   //角色和权限
        setCheckGroupAndCheckItemMenu(roleId,checkitemIdsMenu);   //角色和菜单

    }

    //根据角色id获取角色对象
    @Override
    public Role findById(Integer id) {
        return roleDao.findById(id);
    }


    //向t_role_permission保存数据
    private void setCheckGroupAndCheckItem(Integer roleId, Integer[] checkitemIds) {
        if(checkitemIds != null){
            for (Integer checkItemId : checkitemIds) {
                Map map = new HashMap();
                map.put("roleId",roleId);
                map.put("permissionId",checkItemId);
                roleDao.setCheckGroupAndCheckItem(map);
            }
        }
    }
    //向t_role_menu保存数据  菜单
    private void setCheckGroupAndCheckItemMenu(Integer roleId, Integer[] checkitemIdsMenu) {
        if(checkitemIdsMenu != null){
            for (Integer checkItemId : checkitemIdsMenu) {
                Map map = new HashMap();
                map.put("roleId",roleId);
                map.put("menuId",checkItemId);
                roleDao.setCheckGroupAndCheckItemMenu(map);
            }
        }
    }

    /**
     * 根据roleId查询权限项的ids
     *
     * @param checkGroupId
     * @return
     */
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupId(Integer checkGroupId) {
        return roleDao.findCheckItemIdsByCheckGroupId(checkGroupId);

    }

    //根据roleId查询菜单项的ids
    @Override
    public List<Integer> findCheckItemIdsByCheckGroupIdMenu(Integer checkGroupId) {
        return roleDao.findCheckItemIdsByCheckGroupIdMenu(checkGroupId);
    }


    //更新:
    @Override
    public void edit(Role role, Integer[] checkitemIds, Integer[] checkitemIdsMenu) {
        //1.调用dao 更新role(t_role)
        roleDao.edit(role);
        //2.调用dao 先把role关联的权限项给删除(t_role_permission)
        roleDao.deleteAssociation(role.getId());
        //3.调用dao 把更新提交过来的checkitemIds 再进行关联
        setCheckGroupAndCheckItem(role.getId(),checkitemIds);

        //4. 调用dao 先把role关联的菜单项给删除(t_role_menu):
        roleDao.deleteAssociationMenu(role.getId());
        //5. 调用dao 把更新提交过来的checkitemIds 再进行关联
        setCheckGroupAndCheckItemMenu(role.getId(),checkitemIdsMenu);
    }

    //删除角色.
    @Override
    public void delete(Integer checkGroupId) {

        // 删除中间表权限数据
        roleDao.delet(checkGroupId);
        //删除中间表菜单数据
        roleDao.deletMenu(checkGroupId);
        // 删除角色数据:
        roleDao.deleteCheckGroupId(checkGroupId);
    }

    @Override
    public List<Role> findAll() {
        return roleDao.findAll();
    }
}
